package ${package}.rest.controller;

import javax.annotation.Resource;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.openbpm.base.rest.BaseController;

import org.openbpm.base.api.aop.annotion.CatchErr;
import org.openbpm.base.api.response.impl.ResultMsg;
import ${package}.core.manager.${relation.upperFirstTableKey}Manager;
import ${package}.core.model.${relation.upperFirstTableKey};

<#if  busDataPersistenceRestAPI>
import org.openbpm.base.core.util.StringUtil;
import org.openbpm.bus.api.remote.RemoteBusinessData;
import com.alibaba.fastjson.JSON;
</#if>
/**
 * ${relation.tableComment} 控制层<br/>
 * @author ${author}
 * @email ${email}
 * @time ${currentDate}
 */
@RestController
@RequestMapping("/${system}/${relation.tableKey}")
public class ${relation.upperFirstTableKey}Controller extends BaseController<${relation.upperFirstTableKey}>{
	@Resource
	${relation.upperFirstTableKey}Manager ${relation.tableKey}Manager;



	<#if  busDataPersistenceRestAPI>
	/**
	 * 远程业务对象持久化 保存接口
	 * @param remoteBusinessData
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("saveData")
	@CatchErr(value = "saveData 异常")
	public ResultMsg<String> saveData(@RequestBody RemoteBusinessData<${relation.upperFirstTableKey}> remoteBusinessData) throws Exception {
		LOG.debug("saveData remoteBusinessData:{}", JSON.toJSONString(remoteBusinessData));

		${relation.upperFirstTableKey} ${relation.tableKey} = remoteBusinessData.getData();
		if (StringUtil.isEmpty(${relation.tableKey}.getId())) {
			${relation.tableKey}Manager.create(${relation.tableKey});
		} else {
			${relation.tableKey}Manager.update(${relation.tableKey});
		}

		return getSuccessResult(${relation.tableKey}.getId());
	}


	/**
	 * 远程业务对象持久化 加载数据接口
	 * @param remoteBusinessData
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("loadData")
	@CatchErr(value = "loadData异常")
	public ResultMsg<RemoteBusinessData<${relation.upperFirstTableKey}>> loadData(@RequestBody RemoteBusinessData remoteBusinessData) throws Exception {
		LOG.info("loadData remoteBusinessData:{}", JSON.toJSONString(remoteBusinessData));

		${relation.upperFirstTableKey} ${relation.tableKey} = ${relation.tableKey}Manager.get(String.valueOf(remoteBusinessData.getId()));

		RemoteBusinessData<${relation.upperFirstTableKey}> data = new RemoteBusinessData<>();
		data.setData(${relation.tableKey});

		return getSuccessResult(data);
	}


	/**
	 * 远程业务对象持久化 删除
	 * @param remoteBusinessData
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("removeData")
	@CatchErr(value = "removeData异常")
	public ResultMsg<String> removeData(@RequestBody RemoteBusinessData remoteBusinessData) throws Exception {
		LOG.info("removeData remoteBusinessData:{}", JSON.toJSONString(remoteBusinessData));

		${relation.tableKey}Manager.remove(String.valueOf(remoteBusinessData.getId()));

		return getSuccessResult();
	}

	</#if>


	@Override
	protected String getModelDesc() {
		return "${relation.tableComment}";
	}

}
